package com.woohua.thread.product;

/***
 * @title Producer
 * @description
 * @author woo hua
 * @version 1.0.0
 * @create 2023/3/14 11:35
 **/
public abstract class Producer<T> implements Runnable {

    private BlockedQueue<T> taskQueue;

    public Producer(BlockedQueue<T> taskQueue) {
        this.taskQueue = taskQueue;
    }

    @Override
    public void run() {
        while (true) {
            T[] tasks = generateTask();
            if(null != tasks && tasks.length > 0) {
                for (T task : tasks) {
                    if(null != task) {
                        this.taskQueue.enq(task);
                    }
                }
            }
        }
    }

    /**
     * 生成任务，使用了“模板方法”设计模式，子类子要实现次方法即可
     * @return
     */
    public abstract T[] generateTask();
}
